package leetcode;


public class ATM {

    public int twenty = 0;
    public int fifty = 0;
    public int oneH = 0;
    public int twoH = 0;
    public int fiveH = 0;

    public ATM() {}

    public void deposit(int[] banknotesCount) {
        twenty += banknotesCount[0];
        fifty += banknotesCount[1];
        oneH += banknotesCount[2];
        twoH += banknotesCount[3];
        fiveH += banknotesCount[4];
    }

    public int[] withdraw(int amount) {
        if (amount % 10 != 0) return new int[]{-1};
        int twentyCnt = twenty;
        int fiftyCnt = fifty;
        int oneHCnt = oneH;
        int twoHCnt = twoH;
        int fiveHCnt = fiveH;
        if (fiveH > 0 && amount >= 500) {
            int cnt = amount / 500;
            if (cnt > fiveH) cnt = fiveH;
            fiveH -= cnt;
            amount -= cnt * 500;
        }
        if (twoH > 0 && amount >= 200) {
            int cnt = amount / 200;
            if (cnt > twoH) cnt = twoH;
            twoH -= cnt;
            amount -= cnt * 200;
        }
        if (oneH > 0 && amount >= 100) {
            int cnt = amount / 100;
            if (cnt > oneH) cnt = oneH;
            oneH -= cnt;
            amount -= cnt * 100;
        }
        if (fifty > 0 && amount >= 50) {
            int cnt = amount / 50;
            if (cnt > fifty) cnt = fifty;
            fifty -= cnt;
            amount -= cnt * 50;
        }
        if (twenty > 0 && amount >= 20) {
            int cnt = amount / 20;
            if (cnt > twenty) cnt = twenty;
            twenty -= cnt;
            amount -= cnt * 20;
        }
        if (amount != 0) {
            fiveH = fiveHCnt;
            twoH = twoHCnt;
            oneH = oneHCnt;
            fifty = fiftyCnt;
            twenty = twentyCnt;
            return new int[]{-1};
        }
        return new int[]{twentyCnt - twenty, fiftyCnt - fifty, oneHCnt - oneH, twoHCnt - twoH,fiveHCnt - fiveH};
    }
}